package com.ssm.study.utils;

import java.util.*;

import org.apache.log4j.Logger;

/**
 * Created by bevin on 2017/4/29.
 */

public class GameCore {

    private static Logger logger = Logger.getLogger(GameCore.class);

    //记录供应链的顺序，用于遍历字典
    private static List<Role.roleTypeEnum> roleOrder = Arrays.asList(Role.roleTypeEnum.PRODUCER,
            Role.roleTypeEnum.WHOLESALER, Role.roleTypeEnum.DISTRIBUTOR, Role.roleTypeEnum.DEALER);

    //游戏的唯一标识id
    private String uuid;

    //单位货物进价与售价差值
    private float diffSellingPrice;

    //每单位货物的存储成本(周)
    private float storeCost;

    //每单位货物的缺货成本(周)
    private float stockoutCost;

    //组名
    private String groupName;

    //是否使用随机需求
    private Boolean randomRequirementFlag;

    //正态分布均值
    private float mean=100;

    //正态分布方差
    private float variances=10;

    //游戏中的角色数目
    private Integer roleNum;

    //周数tick
    private Integer tick;

    //供应链的成员Map,此版本List长度为1
    private HashMap<Role.roleTypeEnum, ArrayList<Role>> roles;

    //当前需求
    private HashMap<Role, Integer> currentRequirement;

    //下一周的需求
    private HashMap<Role, Integer> nextRequirement;

    //需求历史
    private HashMap<Integer, HashMap<Role, Integer>> historyRequirements;

    public GameCore(float diffSellingPrice, float storeCost, float stockoutCost, String groupName, Boolean randomRequirementFlag){

        uuid = UUID.randomUUID().toString().replace("-","");

        this.diffSellingPrice = diffSellingPrice;

        this.storeCost = storeCost;

        this.stockoutCost = stockoutCost;

        this.groupName = groupName;

        currentRequirement = null;

        nextRequirement = null;

        tick = 0;

        this.randomRequirementFlag = randomRequirementFlag;

        logger.info("game initialized");
    }

    //设置高斯分布的均值和方差，必须为正数
    public void setRandomParams(float mean, float variances){
        if (randomRequirementFlag){
            if(mean>=0 && variances>0){
                this.mean = mean;
                this.variances = variances;
            }
            else{
                logger.warn("mean and variances must be positive");
            }
        }
        else{
            logger.warn("current setting do not support random parameters");
        }
    }

    //生成大于0的服从(mean, variances)分布的整数
    private int gaussian(){
        Random rand = new Random();
        int randInt;
        while (true){
            randInt = (int) (Math.sqrt(variances)*rand.nextGaussian()+mean);
            if (randInt > 0){
                break;
            }
        }
        return randInt;
    }

    //针对当前版本的 每个角色数量为1 的初始化role的方法
    public void initRoles(String producerName, String wholesalerName, String distributorName, String dealerName){

        roles = new HashMap<Role.roleTypeEnum, ArrayList<Role>>();

        ArrayList<Role> producerList = new ArrayList();
        Role producer = new Role(uuid, Role.roleTypeEnum.PRODUCER, producerName);
        producerList.add(producer);
        roles.put(Role.roleTypeEnum.PRODUCER, producerList);

        ArrayList<Role> wholesalerList = new ArrayList();
        Role wholesaler = new Role(uuid, Role.roleTypeEnum.WHOLESALER, wholesalerName);
        wholesalerList.add(wholesaler);
        roles.put(Role.roleTypeEnum.WHOLESALER, wholesalerList);

        ArrayList<Role> distributorList = new ArrayList();
        Role distributor = new Role(uuid, Role.roleTypeEnum.DISTRIBUTOR, distributorName);
        distributorList.add(distributor);
        roles.put(Role.roleTypeEnum.DISTRIBUTOR, distributorList);

        ArrayList<Role> dealerList = new ArrayList();
        Role dealer = new Role(uuid, Role.roleTypeEnum.DEALER, dealerName);
        dealerList.add(dealer);
        roles.put(Role.roleTypeEnum.DEALER, dealerList);

        //设置role双向链表
        producer.setNextRole(wholesaler);
        wholesaler.setPreviousRole(producer);

        wholesaler.setNextRole(distributor);
        distributor.setPreviousRole(wholesaler);

        distributor.setNextRole(dealer);
        dealer.setPreviousRole(distributor);

        roleNum = 4;

        logger.info("roles initialized");
    }

    //生成随机需求
    public void randomRequirement(){

        nextRequirement = new HashMap<Role, Integer>();

        for(Role.roleTypeEnum key:roles.keySet()){
            for(Role role:roles.get(key)){
                int randInt = gaussian();
                nextRequirement.put(role, randInt);
                role.requirements.put(tick, randInt);
            }
        }
        logger.info("random requirements generated");

    }


    //搜集需求,从数据库,返回是否搜集所有需要的数据
    public boolean collectRequirement(){
        //
        return isRequirementReady();
    }

    public boolean isRequirementReady(){
        return nextRequirement!=null && roleNum.equals(nextRequirement.size());
    }


    public void nextTick(){
        if (!isRequirementReady()){
            logger.warn("Game next requirement not ready, please retry later.");
            return;
        }

        for(Role.roleTypeEnum roleType:roleOrder){
            for (Role role:roles.get(roleType)){
                    //role.nextTick();
                    //role.setCurrentChainData();
            }

        }


        ++tick;
        currentRequirement = nextRequirement;
        nextRequirement = null;
    }


}
